package xcu.lxj.nettychat.chat.dao;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xcu.lxj.nettychat.chat.domain.entity.RoomFriend;
import xcu.lxj.nettychat.chat.mapper.RoomFriendMapper;
import xcu.lxj.nettychat.common.domain.enums.NormalOrNoEnum;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author XinJian Lei
 * @Date 2024/9/28 20:23
 * @Version 1.0
 */

@Service
public class RoomFriendDao extends ServiceImpl<RoomFriendMapper, RoomFriend> {

    /**
     * 通过房间id 获得房间
     * @param roomId
     * @return
     */
    public RoomFriend getByRoomId(Long roomId){
        return lambdaQuery()
                .eq(RoomFriend::getRoomId,roomId)
                .one();
    }

    /**
     * 通过key 获得房间
     * @param key uid1_uid2
     * @return
     */
    public RoomFriend getByKey(String key) {
        return lambdaQuery().eq(RoomFriend::getRoomKey, key).one();
    }

    /**
     * 开启房间
     * @param id
     */
    public void restoreRoom(Long id) {
        lambdaUpdate()
                .eq(RoomFriend::getId, id)
                .set(RoomFriend::getStatus, NormalOrNoEnum.NORMAL.getStatus())
                .update();
    }
    /**
     * 禁用房间
     * @param key uid1_uid2
     */
    public void disableRoom(String key) {
        lambdaUpdate()
                .eq(RoomFriend::getRoomKey, key)
                .set(RoomFriend::getStatus, NormalOrNoEnum.NOT_NORMAL.getStatus())
                .update();
    }
    /**
     * 通过roomId 列表 获取RoomFriend list
     * @param roomIds
     * @return
     */
    public List<RoomFriend> listByRoomIds(List<Long> roomIds) {
        return lambdaQuery()
                .in(RoomFriend::getRoomId, roomIds)
                .list();
    }

    public Map<Long, RoomFriend> getBatch(List<Long> roomIds) {

        return lambdaQuery()
                .in(RoomFriend::getRoomId,roomIds)
                .list()
                .stream()
                .collect(Collectors.toMap(RoomFriend::getRoomId,roomFriend -> roomFriend));
    }
}
